package com.jiezheng.huinong.action;

import java.util.List;
import java.util.Map;

import com.jiezheng.huinong.biz.ApplyManage;
import com.jiezheng.huinong.dao.ApplyManageDAOImpl;
import com.jiezheng.huinong.dao.IndexManageDAOImpl;
import com.jiezheng.huinong.dao.PaperManageDAOImpl;
import com.jiezheng.huinong.model.IndexInfo;
import com.jiezheng.huinong.model.OperationStatus;
import com.jiezheng.huinong.util.CommonUtils;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class IndexManageAction extends ActionSupport {

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = -5530539795612889920L;

	// 所有的指标信息
	private IndexInfo idx = new IndexInfo();

	// 实拨编号
	private String shiboNo = "";

	// 所有的指标ID列表：以indexNo:budgetUnit:budgetItem;形式存储
	private String idList = "";

	private static final int ID_FIELDS_COUNT = 3;

	/**
	 * 增加指标
	 * 
	 * @return
	 */
	public String addIndex() {

		// 对指标的存在性进行判定
		List<String> _indexNo = (new PaperManageDAOImpl()).listIndexNo();
		int pos = _indexNo.indexOf(idx.getIndexNo());
		if (pos == -1) {
			System.out.println("Index no is not exist!");
			return SUCCESS;
		}

		// 增加新指标
		Map<String, Object> session = ActionContext.getContext().getSession();
		idx.setDeptNo((String) session.get("deptNo"));
		idx.setOperator((String) session.get("user"));
		idx.setCreateTime(CommonUtils.getCurrentDate());
		idx.setStatus(OperationStatus.TO_AUDIT);
		idx.setSpareMoney(idx.getMoney());
		(new IndexManageDAOImpl()).addIndex(idx);
		return SUCCESS;
	}

	/**
	 * 删除指标
	 * 
	 * @return
	 */
	public String deleteIndex() {
		(new IndexManageDAOImpl()).deleteIndex(idx);
		return SUCCESS;
	}

	/**
	 * 修改指标
	 */
	public String preModifyIndex() {
		idx = (new IndexManageDAOImpl()).getIndexInfo(idx);
		return SUCCESS;
	}

	/**
	 * 修改指标
	 */
	public String modifyIndex() {
		idx.setSpareMoney(idx.getMoney());
		idx.setStatus(OperationStatus.TO_AUDIT);
		(new IndexManageDAOImpl()).modifyIndex(idx);
		return SUCCESS;

	}

	/**
	 * 指标详情
	 */
	public String detailIndex() {
		idx = (new IndexManageDAOImpl()).getDetailIndexInfo(idx);
		return SUCCESS;
	}

	/**
	 * 实拨处理指标
	 */
	public String shiboIndex() {
		shiboNo = ApplyManage.getMaxShiBoNo();
		idx = (new IndexManageDAOImpl()).getDetailIndexInfo(idx);
		return SUCCESS;
	}
	
	/**
	 * 更新实拨编号
	 */
	public String refreshShiboNo() {
		(new ApplyManageDAOImpl()).updateShiboNo(shiboNo);
		return SUCCESS;
	}

	/**
	 * 送审居民信息
	 * 
	 * @return
	 */
	public String toAudit() {
		audit(OperationStatus.NO_AUDIT);
		return SUCCESS;
	}

	/**
	 * 审核通过指标信息
	 * 
	 * @return
	 */
	public String approveAudit() {
		audit(OperationStatus.AUDITED);
		return SUCCESS;
	}

	/**
	 * 审核不通过指标信息
	 * 
	 * @return
	 */
	public String rejectAudit() {
		audit(OperationStatus.REJECTED);
		return SUCCESS;
	}

	/**
	 * 审核通过指标信息
	 * 
	 * @return
	 */
	public String approveFinalAudit() {
		audit(OperationStatus.FINAL_AUDITED);
		return SUCCESS;
	}

	/**
	 * 审核不通过指标信息
	 * 
	 * @return
	 */
	public String rejectFinalAudit() {
		audit(OperationStatus.REJECTED);
		return SUCCESS;
	}

	public void audit(Integer status) {
		String[] ids = idList.split("[;]");
		for (String id : ids) {
			if (!id.isEmpty()) {
				String[] fields = id.split("[:]");

				// 格式不符合要求
				if (fields.length != ID_FIELDS_COUNT) {
					continue;
				}

				IndexInfo index = new IndexInfo();
				index.setIndexNo(fields[0]);
				index.setBudgetUnitNo(fields[1]);
				index.setBudgetItemNo(fields[2]);
				index.setStatus(status);
				(new IndexManageDAOImpl()).changeStatus(index);
			}
		}
	}

	public IndexInfo getIdx() {
		return idx;
	}

	public void setIdx(IndexInfo idx) {
		this.idx = idx;
	}

	public String getIdList() {
		return idList;
	}

	public void setIdList(String idList) {
		this.idList = idList;
	}

	/**
	 * @return the shiboNo
	 */
	public String getShiboNo() {
		return shiboNo;
	}

	/**
	 * @param shiboNo
	 *            the shiboNo to set
	 */
	public void setShiboNo(String shiboNo) {
		this.shiboNo = shiboNo;
	}
}
